package org.u2ml.app.business.impl;

import java.util.List;

import javax.ws.rs.QueryParam;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import org.u2ml.app.business.OidService;
import org.u2ml.app.domain.Oid;

@Component
@Transactional
public class OidServiceImpl implements OidService {

	@Autowired
	private SessionFactory sessionFactory;
		
	public void deleteOid(Oid oid) {
		sessionFactory.getCurrentSession().delete(oid);
	}

	public Oid saveOid(Oid oid) {
		sessionFactory.getCurrentSession().save(oid);
		return oid;
	}

	public Oid updateOid(Oid oid) {
		sessionFactory.getCurrentSession().update(oid);
		return oid;
	}

	@SuppressWarnings("unchecked")
	public Oid find(@QueryParam("value") Long value) {
		List<Oid> list = sessionFactory.getCurrentSession()
        	.createQuery("from Oid o where o.value = :value")
        	.setParameter("value", value)
        	.list();
		return (list.size() == 0) ? null : list.get(0);
	}

}
